package com.easy.work.push;

import android.content.Context;
import android.content.Intent;

import com.easy.work.MainActivity;
import com.easy.work.utils.StringUtil;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by fangzhu on 17/1/4.
 * 解析推送的action用到的处理类
 * 直接返回intent 到pushReceiver
 */
public class PushActionUtil {
    //公用Str
    public static final String OBJECTID_TACT = "fm_tactics";
    public static final String OBJECTID_TEACHER = "fm_teachers";
    public static final String OBJECTID_THEM = "fm_themes";

    //进入object详情的参数
    public static final String PUSH_P_objectId = "objectId";
    //详情页的id对应
    public static final String PUSH_P_themeId = "themeId";
    //只有牛人的时候需要
    public static final String PUSH_P_objectUserId = "objectUserId";

    //======================================================push的action  start========================================================

    /**
     * 股票推送
     * jingling://openStock?code=sh600000&name=浦发银行
     */
    public static final String ACT_PUSH_OPEN_STOCK = "openStock";
    /**
     * 资讯推送(包括H5页面)
     * jingling://openNews?articleId=24009&title=文章标题&intro=文章描述&h5=http://www.h5.com/xxx.html
     */
    public static final String ACT_PUSH_OPEN_NEWS = "openNews";
    /**
     * 打开bs的指标
     */
    public static final String ACT_PUSH_OPEN_BS = "openBS";

    /**
     * openObject对象内容多一些  需要app再解析一次
     * {"action":"jingling://openObject?objectId=fm_themes@14jingling://openObject?objectId=fm_teachers@123&title=标题&intro=文章描述4&objectUserId=345&title=[风花雪月]发表了一个话题&intro=我发声啦哈哈"},
     * <p/>
     * 策略推送 jingling://openObject?objectId=fm_tactics@123&title=标题&intro=文章描述
     * 牛人推送
     * 话题推送 jingling://openObject?objectId=fm_themes@123&title=标题&intro=文章描述
     */
    public static final String ACT_PUSH_OPEN_OBJECT = "openObject";


    //======================================================push的action  end========================================================

    /**
     * 解析推送数据
     *
     * @param context
     * @param action  jingling://action?p1=21554&p2=大
     * @return intent 直接使用
     */
    public static Intent getIntent(Context context, String action) {
        Intent intent = new Intent();
        //应用开启的时候 需要
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(OpenActivityUtil.IS_GOHOME, true);//返回首页


        //配置参数  参考对应act的start方法
        ActionObj actionObj = OpenActivityUtil.parseActionObj(context, action);
//        if (actionObj != null && !StringUtil.isEmpty(actionObj.getAction())) {
//            String actionMsg = actionObj.getAction();
//            if (ACT_PUSH_OPEN_STOCK.equals(actionMsg)) {
//
//            } else if (ACT_PUSH_OPEN_NEWS.equals(actionMsg)) {
//                intent.setClass(context, WebAct.class);
//                if (actionObj.getParamMap().containsKey("title")) {
//                    intent.putExtra("title", actionObj.getParamMap().get("title"));
//                }
//                String url = "";
//                if (actionObj.getParamMap().containsKey("url")
//                        && !StringUtil.isEmpty(actionObj.getParamMap().get("url"))) {
//                    //如果有url优先使用
//                    url = actionObj.getParamMap().get("url");
//                    intent.putExtra("url", url);
//                } else if (actionObj.getParamMap().containsKey("articleId")) {
//                    //详情是一个网页
//                    Map<String, String> map = new LinkedHashMap<>();
//                    map.put("articleId", actionObj.getParamMap().get("articleId"));
//                    map = ApiConfig.initParam(context, map);
//                    url = ApiConfig.setParam4get(BaseInterface.URL_MIAN_NEWS_DETAIL, map);
//                }
//                if (!StringUtil.isEmpty(url)) {
//                    intent.putExtra("url", url);
//                    return intent;
//                }
//
//            } else if (ACT_PUSH_OPEN_BS.equals(actionMsg)) {
//                //打开bs界面
//                intent.setClass(context, BSIndexAct.class);
//                return intent;
//            } else if (ACT_PUSH_OPEN_OBJECT.equals(actionMsg)) {
//                //themeId是详情的需要的id
//                String objectId = "", themeId = "";
//                if (actionObj.getParamMap().containsKey(PUSH_P_objectId)) {
//                    objectId = actionObj.getParamMap().get(PUSH_P_objectId);
//                    if (objectId.contains("@")) {
//                        themeId = objectId.split("@")[1];
//                    }
//                }
//
//                //是否找到对应的act
//                boolean findAct = true;
//                if (objectId != null) {
//                    if (objectId.startsWith(OBJECTID_TACT)) {
//                        //策略
//                        intent.setClass(context, TactDetailAct.class);
//                    } else if (objectId.startsWith(OBJECTID_TEACHER)) {
//                        //牛人
//                        intent.setClass(context, TeacherDetailAct.class);
//
//                        if (actionObj.getParamMap().containsKey(PUSH_P_objectUserId)) {
//                            //注意这里替换
//                            themeId = actionObj.getParamMap().get(PUSH_P_objectUserId);
//                        }
//                    } else if (objectId.startsWith(OBJECTID_THEM)) {
//                        //社区话题
//                        intent.setClass(context, CommunityDetailAct.class);
//                    } else {
//                        //没有找到act
//                        findAct = false;
//                    }
//                }
//
//                if (findAct
//                        && !StringUtil.isEmpty(objectId)
//                        && !StringUtil.isEmpty(themeId)) {
//                    intent.putExtra(PUSH_P_objectId, objectId);
//                    intent.putExtra(PUSH_P_themeId, themeId);
//                    return intent;
//                }
//            }
//        }
        //如果没有 解析到，直接打开app
        intent.setClass(context, MainActivity.class);
        return intent;
    }
}
